
if (typeof $ === 'undefined') { throw new Error('This application\'s JavaScript requires jQuery'); }

// APP START
// ----------------------------------- 
var version = '20160623001';
var App = angular.module('angle', [
    'ngRoute',
    'ngAnimate',
    'ngStorage',
    'ngCookies',
    'pascalprecht.translate',
    'ui.bootstrap',
    'ui.router',
    'tmh.dynamicLocale',
    'oc.lazyLoad',
    'cfp.loadingBar',
    'ngSanitize',
    'ngResource',
    'ui.utils',
    'ui.grid',
    'ui.grid.pagination',
    'ui.grid.selection',
    'ui.grid.pinning',
    'ui.grid.exporter',
    'ui.grid.edit',
    'ui.grid.autoResize',
    'ui.grid.treeView',
    'ui.grid.resizeColumns',
    'ui.select',
    'ngDialog'
  ]);

App.run(["$rootScope", "$state", "$stateParams",  '$window', '$templateCache', '$http', '$timeout', '$translate', 'Utils',
    function ($rootScope, $state, $stateParams, $window, $templateCache, $http, $timeout, $translate, utils) {
        // Set reference to access them from any scope
        $rootScope.$state = $state;
        $rootScope.$stateParams = $stateParams;
        $rootScope.$storage = $window.localStorage;
        $rootScope.$translate = $translate

        // Uncomment this to disable template cache
        /*$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
            if (typeof(toState) !== 'undefined'){
            $templateCache.remove(toState.templateUrl);
            }
        });*/

        // Scope Globals
        // ----------------------------------- 
        $rootScope.app = {
            name: 'Tanzhi Admin',
            layout: {
                isFixed: true,
                isCollapsed: false,
                isBoxed: false,
                isFloat: false,
                horizontal: false,
                asideHover: false,
                theme: 'app/css/theme-g.css'
            },
            useFullLayout: false,
            hiddenFooter: false,
            viewAnimation: 'ng-fadeIn',
            user: {},
            isSessionValid: false
        };
        
        //直接刷新页面时判定session状态
        $http.get(utils.getBackendPath('/session/isSessionValid'),{withCredentials: true})
        .then(
            function (response) {
                var result = response.data;
                if (utils.isEmpty(result) || result.isValid == 'false'){
                    $rootScope.app.user = {};
                    $rootScope.app.isSessionValid = false;
                    $state.go('page.login');
                }else{
                    $rootScope.app.isSessionValid = true;
                    $rootScope.app.user = {
                        code: result.code,
                        name: localStorage.NG_TRANSLATE_LANG_KEY == 'zh' ? result.cnname : result.enname,
                        zhname: result.cnname,
                        enname: result.enname,
                        faceUrl: utils.isEmpty(result.faceUrl) ? 'app/img/face.jpg' : result.faceUrl
                    };
                }
            }
        );
    }
]);
